//
//  MainTabBar.swift
//  sina weibo
//
//  Created by 万美琳 on 15/12/14.
//  Copyright © 2015年 Eleven. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    //手码调用
   override init(frame: CGRect) {
        
    super.init(frame : frame)
    addSubview(composeBtn)
    }
    //xib／sb初始化调用
   required init?(coder aDecoder: NSCoder) {
//       fatalError("init(coder:) has not been implemented")
    
    super.init(coder: aDecoder)
    addSubview(composeBtn)
   }

    
  override  func layoutSubviews() {
    
    super.layoutSubviews()
    
    let w = self.bounds.width / 5
    let h = self.bounds.height
    
    var index:CGFloat = 0
    
    for subView in subviews {
    //UITabBarButton 是私有属性，不能直接获取，用，所以用转换一下
        if subView .isKindOfClass(NSClassFromString("UITabBarButton")!) {
        
        subView.frame = CGRectMake(index * w, 0, w, h)
//            if index==1 {
//            index++
//            }
        // index++中间不能有空格，否则会报错
            index += (index == 1 ? 2 : 1)
    
        }
        //设置 加号按钮的位置
    composeBtn.frame = CGRectMake(2 * w, 0, w, h)
    
    }
    
  }
   
    //懒加载注意后面要加()
    lazy var composeBtn : UIButton = {
    
    let btn = UIButton()
        
        btn.setImage(UIImage(named:"tabbar_compose_icon_add" ), forState: .Normal)
        btn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted" ), forState: .Highlighted)
       btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: .Normal)
       btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: .Highlighted)
        
      btn.sizeToFit()
        return btn
    }()
    

}
